/**
 * Created by Administrator on 2017/5/23 0023.
 */
$(function () {
    //切换注册方式;
    $(".main-top a").on("click", function () {
        $(this).parent().addClass("online").siblings("li").removeClass("online");
        // console.log($(".main-top a").index($(this)));
        var click = $(".for-click");
        click.eq($(".main-top a").index($(this))).show().siblings("div").hide();
    });
    //以下是邮箱注册的判断
    var flag = false;
    var flag1 = true;
    var pwdFlag = false;
    var pwdAFlag = false;
    var name = $("#name");
    name.on("blur", function () {
        var theInp = $(this);
        var namew = $(".name-warning");
        var namer = $(".name-right");
        //清除上次的的提醒；
        theInp.parent("li").children("p").remove();
        theInp.css("borderColor", "#e5e5e5");
        namew.hide();
        var txt = theInp.val();
        var reg = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
        if (reg.test(txt)) {
            //当用户输入正确邮箱的时候
            flag = true;
            //创建一个接受验证码的input
            if (flag1) {
                flag1 = false;
                var temptinp = $("<input type='text' placeholder='验证码' data-falg='exist'>");
                temptinp.addClass("infor");
                //创建一个li
                var temptLi = $("<li></li>");
                //添加上一个独一无二的id;
                temptLi.attr("id", "email-yanz")
                //给li添加刚刚创建的input；
                temptLi.append(temptinp).css("position", "relative");
                theInp.parent("li").after(temptLi).end().css("borderColor", "#e5e5e5");
                //创建一个发送验证码的按钮;
                var temptInpY = $("<input type='button' value='获取验证码' class='tempY'>");
                temptLi.append(temptInpY);
                namer.show();
            }

        } else {
            //当用户输入不正确的邮箱时；
            var warning = $("<p>请输入正确的邮箱</p>");
            warning.addClass("red");
            theInp.parent("li").append(warning).end().css("borderColor", "red");
            namew.show();
            namer.hide();
            $("#email-yanz").remove();
            flag1 = true
        }
    });
    //密码框;
    $("#pwd,#phone-pwd").on("blur", function () {
        pwdFlag = false;
        var pwdw = $(".pwd-warning");
        var pwdr = $(".pwd-right");
        //密码为6-18位的字符，不能含空格,不能全为数字。
        var thePwd = $(this);
        //清空上次遗留的错误信息;
        thePwd.parent("li").children("p").remove();
        thePwd.css("borderColor", "#e5e5e5");
        pwdr.hide();
        var txt = thePwd.val();
        //^[a-zA-Z]\w{5,17}$
        var reg = /^\w{6,18}$/;
        var reg1 = /^\d{6,18}/;
        if (!reg.test(txt) || reg1.test(txt)) {
            var warning = $("<p>密码为6-18位的字符，不能含空格,不能全为数字。</p>");
            //错误提醒;
            warning.addClass("red");
            thePwd.parent("li").append(warning).end().css("borderColor", "red");
            pwdw.show();
        } else {
            pwdFlag = true;
            //显示正确
            pwdr.show();
        }
    });
    $("#pwdagain,#phone-pwdagain").on("blur", function () {
        var pwdw = $(".pwdagain-warning");
        var pwdr = $(".pwdagain-right");
        var pwdtxt = $(this).parent("li").prev().children("input").val();
        var pwdA = $(this);
        var txt = pwdA.val();
        //清除遗留的错误信息：
        pwdw.hide();
        pwdA.parent("li").children("p").remove();
        pwdA.css("borderColor", "#e5e5e5");
        //判断是否符合规范；
        if (pwdtxt != txt) {
            var warning = $("<p>密码不一致</p>");
            warning.addClass("red");
            pwdA.parent("li").append(warning).end().css("borderColor", "red");
            pwdr.hide();
            pwdw.show();
        } else {
            pwdr.show();
            pwdAFlag = true;
        }
    });

    //手机验证;
    var phoneFlag = false;
    $("#phone").on("blur", function () {
        var namew = $(".name-warning");
        var namer = $(".name-right");
        var reg = /^[1](3|4|5|7|8|)\d{9}$/;
        var txt = $(this).val();
        $(this).css("borderColor", "#e5e5e5").parent("li").children("p").remove();
        namew.hide();
        //正确;
        if (reg.test(txt)) {
            phoneFlag = true;
            //创建一个接受验证码的input;
            if (flag1) {
                flag1 = false;
                var temptinp = $("<input type='text' placeholder='验证码' data-falg='exist'>");
                temptinp.addClass("infor");
                //创建一个li
                var temptLi = $("<li></li>");
                temptLi.attr("id", "phone-yanz");
                //给li添加刚刚创建的input；
                temptLi.append(temptinp).css("position", "relative");
                $(this).parent("li").after(temptLi).end().css("borderColor", "#e5e5e5");
                //创建一个发送验证码的按钮;
                var temptInpY = $("<input type='button' value='获取验证码' class='tempY'>");
                temptLi.append(temptInpY);
                namer.show();
            }
        } else {
            flag1 = true;
            var warning = $("<p>请输入正确的大陆号码</p>");
            warning.addClass("red");
            $(this).parent("li").append(warning).end().css("borderColor", "red");
            namew.show();
            namer.hide();
            $("#phone-yanz").remove();
        }
    });
    //用户注册协议点击;
    $(".agree").on("click", function (e) {
        $("#agreement-w").show();
    });
    $("#close").on("click", function () {
        $("#agreement-w").hide();
    });
    $("#agreement1").on("click",function () {
        $("#agreement-w").hide();
    });
    //点击邮箱注册按钮的时候;
    $("#email_btn").on("click", function () {
        if (flag && pwdFlag && pwdAFlag) {
            //邮箱和密码的信息;
            $.ajax({
                url: "./server/reg.php",
                type: "post",
                data: {
                    uname: name.val(),
                    upwd: $("#pwd").val()
                },
                success: function (res) {
                    if (JSON.parse(res).status== 1) {

                        alert("注册成功，请登录");
                        window.location.assign("login.html")
                    }
                    if (JSON.parse(res).status== 2) {

                        var theInp = $(name);
                        var namew = $(".name-warning");
                        var namer = $(".name-right");
                        var warning = $("<p>邮箱已注册</p>");
                        warning.addClass("red");
                        theInp.parent("li").append(warning).end().css("borderColor", "red");
                        namew.show();
                        namer.hide();
                        $("#email-yanz").remove();
                        flag1 = true
                    }
                }
            })
        }else {

        }

    })
});
